#include <iostream>
#include <boost/foreach.hpp>
#include "melon/client/thrift_client_pool.h"
#include "MessageHistoryService.h"

using imserver::MessageHistoryServiceClient;
using imserver::ImMessage;
using imserver::MessageResult;
using imserver::ImMessageList;
using melon::client::ClientPool;

void * ThreadFun(void * data) {
  ClientPool<MessageHistoryServiceClient> * pool = (ClientPool<MessageHistoryServiceClient>*)data;

  // const char * addrs[] = {"127.0.0.1:9091", "127.0.0.1:9093"};
  const char * addrs[] = {"127.0.0.1:9091"};
  int addr_count = sizeof(addrs) / sizeof(char*);
  char buffer[64] = {0};
  MessageHistoryServiceClient * client = NULL;

  MessageResult cell;
  std::vector<int64_t> cc;
  //cc.push_back(2477700);
  cc.push_back(2477720);
  //cc.push_back(2477740);
  //cc.push_back(2477760);
  //cc.push_back(2477780);
  //cc.push_back(2477800);
  //cc.push_back(2477820);
  for  (int i = 0; i < 1000; ++i) {
  // for (int i = 0; i < 1; ++i) {
    std::string addr(addrs[i % addr_count]);
    client = pool->Alloc(addr);
    if (!client) {
      continue;
    }

    snprintf(buffer, 63, "Hello World ~ (%d)", i);
    ImMessage message;
   // message.__set_seq_id(1);
   // message.sender = 10001;
   // message.receiver = 10002;
   // message.timestamp = time(NULL);
   // message.body = buffer;
   // message.flags = 1;
      message.__set_seq_id(100);
      message.__set_flags(0x0);
      message.__set_sender(10000);
      message.__set_receiver(117947428);
      message.__set_timestamp(time(NULL));
      message.__set_body(buffer);
    bool success = true;
    try {
      cell.results.clear();
      //client->GetMessages(cell, 10000, 117947428, 75180, 10);
      std::cout << cell.results.size();
      BOOST_FOREACH(const ImMessage& msg, cell.results) {
        std::cout << "( receiver " << msg.receiver << " from "<< msg.sender << " ) with message " << msg.seq_id << std::endl;
      }
      // 离线消息
      if(i % 20 == 0) {
        message.__set_flags(0x1);
      }

      client->AddMessage(message);
      if (i % 20 == 0) {
        std::cout << "=================O f f l i n e     M e s s a g e ==========================" << std::endl;
        cell.results.clear();
        //client->GetOfflineMessages(cell, 117947428, 90, 10);
        BOOST_FOREACH(const ImMessage& msg, cell.results) {
          std::cout << "( receiver " << msg.receiver << " from "<< msg.sender << " ) with message " << msg.seq_id << std::endl;
        }
      }
      if (i % 200 == 0) {
       // client->SetReadByID(cc, 117947428);
      }
    } catch (const std::exception & e) {
      std::cout << addr << " error:" << e.what() << std::endl;
      success = false;
    }
    pool->Release(addr, client, success);
  }
  return NULL;
}

const int kThreadCount = 1;

int main() {
  LOG_INIT("./log", "WARN");
  ClientPool<MessageHistoryServiceClient> pool;

  pthread_t ts[kThreadCount];

  for (int i = 0; i < kThreadCount; ++i) {
    pthread_create(&ts[i], NULL, ThreadFun, &pool);
  }

  for (int i = 0; i < kThreadCount; ++i) {
    pthread_join(ts[i], NULL);
  }
  return 0;
}

